package yl.hs.bmipfull.service.laboratorydata.imple;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.laboratorydata.ILabExperimentDefinitionLibsDao;
import yl.hs.bmipfull.pojo.laboratory.PackageInfoEntity;
import yl.hs.bmipfull.pojo.laboratorydata.LabExperimentDefinitionlibsEntity;
import yl.hs.bmipfull.pojo.material.RawMaterialCateEntity;
import yl.hs.bmipfull.service.laboratory.impl.LaboratoryBaseService;
import yl.hs.bmipfull.service.laboratorydata.ILabExperimentDefinitionLibsService;
import yl.hs.bmipfull.utils.SearchFilterEngine;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.List;
import java.util.Map;

/**
 * 作用:   <br/>
 * 创建时间: 2022-06-20 10:05 <br/>
 *
 * @author 吴高丰
 */
@Service
public class LabExperimentDefinitionLibsServerImple extends LaboratorydataBaseService implements ILabExperimentDefinitionLibsService {
    @Autowired
    ILabExperimentDefinitionLibsDao dao;
    @Override
    public List<LabExperimentDefinitionlibsEntity> queryList(PageQuery pageQuery) throws Exception {
        pageQuery.checkSortSides(LabExperimentDefinitionlibsEntity.class);
        pageQuery.getQuery().put("sortKey", String.join(",", pageQuery.getSortSides()));
        pageQuery.getQuery().put("unitSNArray", getCompanySNList());
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(), LabExperimentDefinitionlibsEntity.class);
        return dao.queryList(pageQuery.getQuery(), filters);
    }

    @Override
    public PageResult<LabExperimentDefinitionlibsEntity> queryPage(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(LabExperimentDefinitionlibsEntity.class);
        pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
        pageQuery.getQuery().put("pageSize", rowNum);
        PageResult<LabExperimentDefinitionlibsEntity> result = new PageResult(rowNum);
        var filters = SearchFilterEngine.generateQueryList(pageQuery.getFilters(),LabExperimentDefinitionlibsEntity.class);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery(),filters));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery(),filters));
        return result;
    }

    @Override
    public LabExperimentDefinitionlibsEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Override
    public Integer saveLabExperimentDefinitionlibs(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        LabExperimentDefinitionlibsEntity entity = new LabExperimentDefinitionlibsEntity();
        if (Tl.isEmpty(sn)) {
            //执行保存
            var changelog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(LabExperimentDefinitionlibsEntity.class));
            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), changelog, Tl.toJson(entity), PackageInfoEntity.class);
            }

        } else {
            //执行修改
            entity = findBySn(sn);
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            result = dao.update(entity);
            if (result > 0) {
                writeLog(ActionType.UPDATE, sn, "更新标准库信息信息", Tl.toJson(entity), PackageInfoEntity.class);
            }
        }
        return result;
    }

    @Override
    public Integer UpdateStateBySN(String sn, int state) {
        return dao.UpdateStateBySN(sn, state);
    }
}
